import { CharHandle, FuncHandle, GaRegExp } from "../rule";

{
	// \\ 转义字符
	const rule = new GaRegExp(["\\(\\)"]);
	console.log(rule.Test("()")); // true
	debugger;
}

{
	// [] 字符组 [^] 反字符组
	const rule = new GaRegExp(["[123]"]);
	console.log(rule.Test("1")); // true
	console.log(rule.Test("2")); // true
	console.log(rule.Test("3")); // true
	console.log(rule.Test("4")); // false
	debugger;
}

{
	// () 规则组 | 条件分支
	const rule = new GaRegExp(["^", "123|456|(789|7)", "$"]);
	console.log(rule.Test("123")); // true
	console.log(rule.Test("456")); // true
	console.log(rule.Test("789")); // true
	console.log(rule.Test("7")); // true
	console.log(rule.Test("78")); // false
	debugger;
}

{
	// {} 匹配个数 *+?
	const rule = new GaRegExp(["^", "a*b{1,2}", "$"]);
	console.log(rule.Test("aaab")); // true
	console.log(rule.Test("abb")); // true
	console.log(rule.Test("b")); // true
	console.log(rule.Test("a")); // false
	debugger;
}

{
	// - 字符区间 \u0000 元字符
	const rule = new GaRegExp(["\\u0061-\\u007a"]);
	console.log(rule.Test("a")); // true
	console.log(rule.Test("b")); // true
	console.log(rule.Test("z")); // true
	console.log(rule.Test("A")); // false
	debugger;
}

{
	// CharHandle 字符处理函数
	const ruleData: CharHandle = (save, origin, rule, str, index, char, length) => char === "5";
	const rule = new GaRegExp([ruleData, "+"]);
	console.log(rule.Test("5")); // true
	console.log(rule.Test("55")); // true
	console.log(rule.Test("6")); // false
	debugger;
}

{
	// FuncHandle 执行函数
	const ruleData: FuncHandle = (save, origin, rule, str, index, char, length) => {
		console.log("test");
	};
	const rule = new GaRegExp([{ ruleType: "func", ruleData }, "123"]);
	console.log(rule.Test("123")); // test true
	debugger;
}

{
	// position 匹配位置
	const ruleData: CharHandle = (save, origin, rule, str, index, char, length) => {
		return index === 3;
	};
	const rule = new GaRegExp(["^", "123{1,2}", { ruleType: "position", ruleData }, "456", "$"]);
	console.log(rule.Test("123456")); // true
	console.log(rule.Test("1233456")); // false
	debugger;
}

{
	// once (~) 所有分支只匹配一次
	const ruleA = new GaRegExp(["^(\\s*abc\\s*)*$"]);
	try {
		console.log(ruleA.Test(" abc  abc ")); // error 存在多分支满足条件
	} catch (error) {
		console.log(error.message);
	}
	const ruleB = new GaRegExp(["^((~\\s*)abc(~\\s*))*$"]);
	console.log(ruleB.Test(" abc  abc ")); // true
	debugger;
}

{
	// nested 正则嵌套-自我嵌套
	const ruleA = new GaRegExp(["7"]);
	const ruleB = new GaRegExp();
	ruleB.ParseRule([ruleA, "(", ruleB, "|4)"]);
	console.log(ruleB.Test("74")); // true
	console.log(ruleB.Test("774")); // true
	console.log(ruleB.Test("7774")); // true
	console.log(ruleB.Test("4")); // false
	debugger;
}

{
	// (!) 捕获字符串
	const rule = new GaRegExp(["(!a)((!c))((!d))*(&(!e))*((-(!f))*)(!b)"]);
	console.log(rule.MatchFormat("acddeeffb"));
	// ["a",["c"],[["d"],["d"]],["e"],["e"],["f","f"],"b"]
	debugger;
}

{
	// 补充捕获内容
	const ruleData: FuncHandle = (save, origin, rule, str, index, char, length) => {
		GaRegExp.Supply(origin, "补充的内容");
		GaRegExp.Supply(origin, ["补充一组内容1", "补充一组内容2"]);
	};
	const rule = new GaRegExp(["^", { ruleType: "func", ruleData }, "(!456)", "$"]);
	console.log(rule.MatchFormat("456")); // ["补充的内容",["补充一组内容1","补充一组内容2"],"456"]
	debugger;
}
